;(function() {

    // Browser environment sniffing
    var inBrowser = typeof window !== 'undefined'
    var UA = inBrowser && window.navigator.userAgent.toLowerCase()
    var isIE = UA && /msie|trident/.test(UA)
    var isIE9 = UA && UA.indexOf('msie 9.0') > 0
    var isEdge = UA && UA.indexOf('edge/') > 0
    var isAndroid = UA && UA.indexOf('android') > 0
    var isIOS = UA && /iphone|ipad|ipod|ios/.test(UA)
    var isWechat = UA && UA.indexOf('micromessenger') > 0
    var isChrome = window.navigator.userAgent.indexOf("Chrome") !== -1;
    var isQQ = UA && UA.match(/MQQBrowser\/([\d.]+)/)
    var isTBS = UA.indexOf('tbs') > 0 ? UA.match(/tbs\/([\d.]+)/) : UA.match(/TBS\/([\d.]+)/)

    // Transition property/event sniffing
    var transitionProp = 'transition'
    var transitionEndEvent = 'transitionend'
    var animationProp = 'animation'
    var animationEndEvent = 'animationend'
    if (window.ontransitionend === undefined && window.onwebkittransitionend !== undefined) {
        transitionProp = 'WebkitTransition'
        transitionEndEvent = 'webkitTransitionEnd'
    }
    if (window.onanimationend === undefined && window.onwebkitanimationend !== undefined) {
        animationProp = 'WebkitAnimation'
        animationEndEvent = 'webkitAnimationEnd'
    }

    var firClickTime =0;
    var endAni;
    var loadimgarr = [];
    var endImgs = [];
    var	endShare = {
        	init:function(){
        		var img_list1 = ['img/endshare/20.jpg','img/endshare/21.jpg','img/endshare/22.jpg',
        		'img/endshare/23.jpg','img/endshare/24.jpg','img/endshare/25.jpg','img/endshare/26.jpg','img/endshare/27.jpg'];
    //     		endAni = loopImgCanvas.create({//创建实例1
				//     parent:'.canvas-wrap',
				//     imgarr:img_list1,
				//     msPerFrame:60
				// });
				for(i=0,len=img_list1.length;i<len;i++){
					endImgs[i] = new Image();
					endImgs[i].src = img_list1[i];
				}
        	},
        	play:function(){
        		// endAni.play();
        		var i = 0;
        		var $s = $('#share img');
        		clearInterval(endAni);
        		endAni = setInterval(function(){
        			$s.attr('src',endImgs[i].src);
        			if(i>=endImgs.length-1){
        				i=0;
        			}else{
        				i++;
        			}
        		},50);
        	},
        	stop:function(){
        		// endAni.stop();
        		clearInterval(endAni);
        	},
        },
        methods = {
            orientation: function() {

                var $win = $(window)
                var $html = $('html')
                var $tips = $('#tips')

                function change() {
                    if (window.orientation == 90 || window.orientation == -90) {
                        setTimeout(function() {
                            $html.css('font-size', $win.height() / 622 * 625 + '%')
                        }, 300)
                        $tips.show()
                    } else {
                        setTimeout(function() {
                            $html.css('font-size', $win.width() / 750 * 625 + '%')
                        }, 300)
                        $tips.hide()
                    }
                }

                change()
                $win.on('onorientationchange' in window ? 'orientationchange' : 'resize', change)

            },
            loading: function() {
                var $video = $('.video')
                var loader = new PxLoader()

                ;[
                    'img/arr.png',
                    'img/bg_default.jpg',
                    'img/b_enter.png',
                    'img/bg_end.jpg',
                    'img/bg_start.jpg',
                    'img/light.png',
                    'img/replay_btn.png',
                    'img/share.jpg',
                    'img/share_btn.png',
                    'img/i_tips.png'
                ].forEach(function(i) {
                    loader.add(new PxLoaderImage(i))
                })

                for (var i = 0; i <= 109; i++) {
                    loadimgarr[i] = loader.addImage('img/loading/loading_000' + i + '.png')
                }
                    

                //loader.addProgressListener(function(e) {
                //    var percent = Math.floor(e.completedCount / e.totalCount * 100)
                //})

                loader.addCompletionListener(function() {
                    methods.animationFrame()
                })

                loader.start()


            },
            video: function() {
                
                var $loading = $('#loading')
                var $btnEnter = $loading.find('.btn-enter')
                var $player = $('#player')
                var $light = $('.logo-light-wrap')
                var $video = $('#video')[0]
                var $bgm = $('#bgm')[0]
                var $end = $('#end')
                var duration;
                var st;
                var canPlayEnd = true;

                function playStart() {
                    $btnEnter.hide()
                    $loading.removeClass('go')
                    $loading.addClass('go')
                    $light.removeClass('ani ani2')
                    $video.play()
                    $video.pause()
                    $bgm.play()
                    $bgm.pause()
                    // $('.loading-line').hide();
                    // console.log('playStart1')
                    setTimeout(function() {
                        $loading.hide();
                        $player.show();
                        $video.play();
                        $end.removeClass('show');
                        $loading.removeClass('go');
                        $('.progress-3').css('opacity','0');
                        // console.log('playStart2')
                    }, 600)
                    setTimeout(function(){
                        // $player.hide()
                        $('.loading-line').hide();
                        // $end.show();
                        // $end.addClass('show')
                    },5000)
                }

                function playEnd() {
                    if(!canPlayEnd){return;}
                    // console.log('playEnd')
                    canPlayEnd = false;
                    $end.show();

                    setTimeout(function(){
                        $player.hide()
                        $video.pause()
                        endShare.init();
                        $end.addClass('show')
                    },300)

                    st = setTimeout(function() {
                        $bgm.play()
                    }, 1000) // 2000
                }

                function rePlay(){
                    canPlayEnd = true;
                    $video.currentTime = 0;
                    clearTimeout(st);
                    playStart();
                    $bgm.pause();
                    // console.log('rePlay')
                }

                // fix iOS9 playsinline
                if (isIOS) {
                    enableInlineVideo($video)
                }

                // if (isIOS) {
                //     makeVideoPlayableInline($video)
                // }
                // var canp=true;
                $($video).on('timeupdate', function() {
                    if (!duration) {
                        duration = $video.duration
                    } else {
                    	if (duration - $video.currentTime <= 30) endShare.init()
                        // if (duration - $video.currentTime <= 110&&canp){
                        //     $video.currentTime =113;
                        //     canp=false;
                        // }
                            
                        if (duration - $video.currentTime <= 1) playEnd()
                        // if (duration - $video.currentTime <= 3) $video.pause();
                    }
                })

                // $btnEnter.on('click', function() {
                //     playStart()
                // })
                
                // $('.btn-1').on('touchstart', function() {
                //     // rePlay();
                //     location.reload();
                // })

                methods.video.playStart = playStart;
            },
            end: function() {

                var $share = $('#share')

                $('.btn-2').on('touchstart', function() {
                    $share.show()
                    endShare.play();
                })

                $share.on('touchstart', function() {
                    $share.hide()
                    endShare.stop();
                })

                
            },
            bindEvent : function(){
                $(window).on('touchmove',function(e){
                    e.preventDefault();
                });

                if(isIOS){
                    $(window).on('touchstart',function(e){
                        var curr = +new Date()
                        if(curr - firClickTime < 300){
                            return false;
                        }
                        firClickTime = +new Date();
                    });
                }
            },
            animationFrame: function() {

                // 选择器
                var $light = $('.logo-light-wrap')
                var $loading = $('#loading')
                var $progress1 = $loading.find('.progress-1')
                var $progress2 = $loading.find('.progress-2')
                var $btnEnter = $loading.find('.btn-enter')
                var $loadingLine = $loading.find('.loading-line')
                // var $text = $('.progress-text')
                // var $text1 = $text.find('.progress-text-1 span')
                // var $text2 = $text.find('.progress-text-2')

                // 自定义
                var step1 = 35
                // var step2 = 9
                // var step2 = 110
                var loop = 3
                var speed = 20

                // 初始化
                var current = 0
                // var total = step1 * loop + step2
                var total = step1 * loop
                var percent = 0
                var growth = 100 / total
                var isSwitch = false
                var time
                var num = 0
                // var text = [
                //     '即将登陆神秘星球',
                //     '请系好安全带',
                //     '请开启手机音效',
                //     '带好瓜子',
                //     '带好枪',
                //     '准备嗨起来'
                // ]

                // // 第二版
                // var step1 = 3
                // var step2 = 30
                // var loop = 18
                // // var num = 1
                // // var num = 0;
                // var current = 0
                // var count = step1 + step2

                time = setInterval(function() {
                    requestAnimationFrame(function() {
                        // console.log(current)
                        // 累计
                        current++

                        // 加载显示
                        if ( current < total ) {
                            // 加载进度
                            percent += growth
                            // $text2.text( text[ parseInt( percent / 17 ) ] )
                            // if(num<104){
                                // $loadingLine.css('background-image', 'url(img/loading/loading_000'+Math.floor(current)+'.png)')
                                $loadingLine.css('background-image', 'url('+loadimgarr[Math.floor(current)].src+')')
                            //     num++;
                            // }
                        } else {
                            // 加载成功
                            time = clearInterval(time)
                            percent = 100
                            // $text.hide()
                            // $btnEnter.show()
                            $btnEnter.find('span').html('点击出发')
                            $light.addClass('ani ani2');
                            $loading.addClass('go')
                            $loading.one('click', function() {
                                methods.video.playStart();
                                // console.log(methods.video.playStart)
                            })
                            // console.log($('#main').html())
                        }
                        // $text1.text( parseInt(percent) )
                        
                        /*
                        // 序列帧
                        if( total - step2 >= current ) {
                            // 循环
                            $progress1.css('background-position-y', - num * 12.2 + 'rem')
                            num++
                            if( num >= step1) num = step1 - step1
                        } else {
                            // 切换
                            if(!isSwitch) {
                                isSwitch = true
                                num = 0
                                $loading.addClass('show')
                            }
                            // 结尾
                            $progress2.css('background-position-y', - num * 12.2 + 'rem')
                            num++
                            if( num >= step2) num = step2 - 1
                        }
                        */

                        // 第二版
                        // if ( total - step2 >= current ) {
                        //     // 循环
                        //     $loadingLine.css('background-image', 'url(img/loading/loading_000'+num+'.png)')
                        //     num++
                        //     if (num > step1) num = 1
                        // } else {
                        //     // 切换
                        //     if (!isSwitch) {
                        //         isSwitch = true
                        //         num = step1 + 1
                        //     }
                        //     // 结尾
                        //     $loading.css('background-image', 'url(img/bg_start.jpg)')
                        //     num++
                        //     if (num > count) num = count
                        // }

                    })
                }, speed)

            }
        },
        
        init = function() {
            methods.orientation()
            methods.loading()
            methods.video()
            methods.end()
            methods.bindEvent();
        }()

})()